package com.suixun.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.mybatis.spring.SqlSessionTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.suixun.dao.JfGiftGoodsArchivesMapper;
import com.suixun.dao.JfGiftMapper;
import com.suixun.dao.JfGoodsArchivesMapper;
import com.suixun.dao.JfPutAreaMapper;
import com.suixun.dao.JfSendGoodsRecordMapper;
import com.suixun.dao.JfSendPeopleGiftMapper;
import com.suixun.dao.JfSendPeopleGoodsArchivesMapper;
import com.suixun.dao.JfSendPeopleMapper;
import com.suixun.dao.StdPeopleinfoMapper;
import com.suixun.entity.JfGift;
import com.suixun.entity.JfGiftGoodsArchives;
import com.suixun.entity.JfGoodsArchives;
import com.suixun.entity.JfGoodsArchivesExample;
import com.suixun.entity.JfPutArea;
import com.suixun.entity.JfPutAreaExample;
import com.suixun.entity.JfSendGoodsRecord;
import com.suixun.entity.JfSendPeople;
import com.suixun.entity.JfSendPeopleGift;
import com.suixun.entity.JfSendPeopleGoodsArchives;
import com.suixun.entity.RegionInfo;
import com.suixun.entity.Schoolinfo;
import com.suixun.entity.StdPeopleinfo;
import com.suixun.entity.StdPeopleinfoExample;
import com.suixun.entity.ext.credits.ExtSendGoodsRecord;
import com.suixun.entity.ext.credits.ExtSendPeople;
import com.suixun.entity.form.SendGoodsForm;
import com.suixun.entity.jxt.JxtResult;
import com.suixun.enums.credits.UserRange;
import com.suixun.exception.OpServiceException;
import com.suixun.jxt.service.JxtService;
import com.suixun.queue.SendGoodsDeal;
import com.suixun.service.RegionService;
import com.suixun.service.credits.SendGoodsService;
import com.suixun.vo.SendGoodsVO;

import rop.utils.spring.CollectionUtils;

@Service
public class SendGoodsServiceImpl implements SendGoodsService {

	private final static Logger logger = LoggerFactory.getLogger(SendGoodsServiceImpl.class);

	@Resource
	private JxtService jxtService;
	@Resource
	private JfGiftMapper jfGiftMapper;
	@Resource
	private JfSendGoodsRecordMapper JfSendGoodsRecordMapper;
	@Resource
	private JfGoodsArchivesMapper jfGoodsArchivesMapper;
	@Resource
	private JfGiftGoodsArchivesMapper jfGiftGoodsArchivesMapper;
	@Resource
	private JfSendPeopleMapper jfSendPeopleMapper;
	@Resource
	private JfSendPeopleGiftMapper jfSendPeopleGiftMapper;
	@Resource
	private JfSendGoodsRecordMapper jfSendGoodsRecordMapper;
	@Resource
	private JfSendPeopleGoodsArchivesMapper jfSendPeopleGoodsArchivesMapper;
	@Resource
	private StdPeopleinfoMapper stdPeopleinfoMapper;
	@Resource
	private SqlSessionTemplate sqlSessionTemplate;
	@Resource
	private JfPutAreaMapper jfPutAreaMapper;
	@Resource
	private RegionService regionService;

	@Override
	@Transactional
	public String sendGoodsByRegion(SendGoodsForm sendGoodsForm, Long userId) {
		if (sendGoodsForm == null) {
			return "参数为空";
		}
		if (StringUtils.isBlank(sendGoodsForm.getContent())) {
			return "发送内容为空";
		}
		if (CollectionUtils.isEmpty(sendGoodsForm.getGoodsNumber())) {
			return "商品编号为空";
		}
		if (CollectionUtils.isEmpty(sendGoodsForm.getRegions())) {
			return "投放地区为空";
		}
		if (sendGoodsForm.getContent().length() > 30) {
			return "内容长度超过限制";
		}
		if (sendGoodsForm.getGoodsNumber().size() > 5) {
			return "一次最多只能发放5个商品";
		}
		if (sendGoodsForm.getRange() != null && sendGoodsForm.getRange() != 1 && sendGoodsForm.getRange() != 2) {
			return "发放类型有误";
		}

		// 校验商品编号
		String result = checkGoods(sendGoodsForm.getGoodsNumber());
		if (result != null) {
			return result;
		}

		// 判断输入的商品是否重复
		checkGoodsNumberRepeat(sendGoodsForm.getGoodsNumber());

		// 查询地区下面所有的学校，然后再通过学校查询下面的家长或老师
		Set<String> schoolNameSet = getSchoolNameByRegions(sendGoodsForm.getRegions());
		if (CollectionUtils.isEmpty(schoolNameSet)) {
			return "该地区下没有用户";
		}
		// 获取地区下所有对应用户的手机号
		Set<String> peopletels = getPeopletels(schoolNameSet, sendGoodsForm.getRange());
		if (CollectionUtils.isEmpty(peopletels)) {
			return "该地区下没有用户";
		}
		// 判断商品库存是否充足
		checkGoodsNumbers(sendGoodsForm.getGoodsNumber(), peopletels);

		// 判断该用户是否存在相同的可领优惠券或已领取过相同的优惠券
		checkIeReceived(peopletels, sendGoodsForm.getGoodsNumber());
		// 保存发券记录
		addSendGoodsRecord(sendGoodsForm, userId);

		if (sendGoodsForm.getGoodsNumber().size() == 1) {
			// 发送单个商品
			sendGoodsArchives(peopletels, sendGoodsForm.getContent(), sendGoodsForm.getGoodsNumber().get(0), userId,
					sendGoodsForm.getRange());
		} else {
			// 发送多个商品时，为大礼包
			sendGift(peopletels, sendGoodsForm.getContent(), sendGoodsForm.getGoodsNumber(), userId,
					sendGoodsForm.getRange());
		}
		push(peopletels, sendGoodsForm.getContent(), sendGoodsForm.getGoodsNumber(), sendGoodsForm.getRange());
		return null;
	}

	/**
	 * <p>
	 * Title: checkIeReceived
	 * </p>
	 * <p>
	 * 功能描述: 判断用户是否已经领取该优惠券
	 * </p>
	 * 
	 * @param peopletels
	 */
	private void checkIeReceived(Set<String> peopletels, List<String> goodsNumberList) {
		
		peopletels.removeIf(peopletel -> check(peopletel, goodsNumberList) == false);
		/*for (String peopletel : peopletels) {
			check(peopletel, goodsNumberList);
		}*/
	}

	private boolean check(String peopletel, List<String> goodsNumberList) {
		Map<String, Object> map = new HashMap<>();
		map.put("peopletel", peopletel);
		map.put("goodsNumberList", goodsNumberList);
		if (!CollectionUtils.isEmpty(sqlSessionTemplate.selectList("SendGoodsExtendSql.getPeopleGiftList", map))) {
			// throw new OpServiceException("不能重复发送优惠券给同一个用户");
			return false;
		}
		if (!CollectionUtils
				.isEmpty(sqlSessionTemplate.selectList("SendGoodsExtendSql.getPeopleGoodsArchiveList", map))) {
			// throw new OpServiceException("不能重复发送优惠券给同一个用户");
			return false;
		}
		return true;
	}

	/**
	 * <p>
	 * Title: checkGoodsNumbers
	 * </p>
	 * <p>
	 * 功能描述: 校验发券商品库存是否小于发送人数
	 * </p>
	 * 
	 * @param goodsNumberList
	 * @param peopletelSet
	 */
	private void checkGoodsNumbers(List<String> goodsNumberList, Set<String> peopletelSet) {
		if (CollectionUtils.isEmpty(goodsNumberList)) {
			throw new OpServiceException("发送商品不能为空");
		}
		if (CollectionUtils.isEmpty(peopletelSet)) {
			throw new OpServiceException("发送用户不存在");
		}
		JfGoodsArchives jfGoodsArchives = null;
		for (String goodsNumber : goodsNumberList) {
			jfGoodsArchives = getGoodsArchives(goodsNumber);
			if (peopletelSet.size() > jfGoodsArchives.getActualNum()) {
				throw new OpServiceException(String.format("编号为%s的商品库存不足", goodsNumber));
			}
		}
	}

	/**
	 * <p>
	 * Title: push
	 * </p>
	 * <p>
	 * 功能描述: 发送推送
	 * </p>
	 * 
	 * @param peopletels
	 * @param content
	 * @param goodsNumberList
	 * @param range
	 */
	private void push(Set<String> peopletels, String content, List<String> goodsNumberList, Integer range) {
		SendGoodsVO sendGoodsVo = new SendGoodsVO();
		sendGoodsVo.setContent(content);
		sendGoodsVo.setGoodsNumberList(goodsNumberList);
		sendGoodsVo.setPeopletels(peopletels);
		sendGoodsVo.setRange(range);
		SendGoodsDeal.add(sendGoodsVo);
	}

	/**
	 * 
	 * <p>
	 * Title: addSendGoodsRecord
	 * </p>
	 * <p>
	 * 功能描述: 保存发券记录
	 * </p>
	 * 
	 * @param sendGoodsForm
	 * @param userId
	 */
	private void addSendGoodsRecord(SendGoodsForm sendGoodsForm, Long userId) {
		try {
			ObjectMapper objectMapper = new ObjectMapper();
			JfSendGoodsRecord jfSendGoodsRecord = new JfSendGoodsRecord();
			jfSendGoodsRecord.setAuthUserId(userId.intValue());
			jfSendGoodsRecord.setContent(sendGoodsForm.getContent());
			jfSendGoodsRecord.setCreateTime(new Date());
			jfSendGoodsRecord.setGoodsNumbers(objectMapper.writeValueAsString(sendGoodsForm.getGoodsNumber()));
			jfSendGoodsRecord.setSendRange(UserRange.getName(sendGoodsForm.getRange()));
			if (CollectionUtils.isEmpty(sendGoodsForm.getRegions())) {
				jfSendGoodsRecord.setPeopletels(objectMapper.writeValueAsString(sendGoodsForm.getPeopletels()));
			} else {
				jfSendGoodsRecord.setSendArea(objectMapper.writeValueAsString(sendGoodsForm.getRegions()));
			}
			jfSendGoodsRecordMapper.insertSelective(jfSendGoodsRecord);
		} catch (JsonProcessingException e) {
			logger.error(e.getMessage());
			throw new OpServiceException("发券记录解析异常");
		}
	}

	/**
	 * 
	 * <p>
	 * Title: sendGoodsArchives
	 * </p>
	 * <p>
	 * 功能描述: 发送单个商品
	 * </p>
	 * 
	 * @param peopletels
	 * @param content
	 * @param goodsNumber
	 * @param userId
	 */
	private void sendGoodsArchives(Set<String> peopletels, String content, String goodsNumber, Long userId,
			Integer range) {
		/*
		 * for (String peopletel : peopletels) { final JfSendPeople sendPeople =
		 * addSendPeople(peopletel, content, userId);
		 * addSendPeopleGoodsArchives(sendPeople.getId(),
		 * getGoodsArchives(goodsNumber).getId()); }
		 */
		ExecutorService executor = Executors.newFixedThreadPool(10);
		for (String peopletel : peopletels) {
			executor.execute(new Runnable() {

				@Override
				public void run() {
					final JfSendPeople sendPeople = addSendPeople(peopletel, content, userId, range);
					addSendPeopleGoodsArchives(sendPeople.getId(), getGoodsArchives(goodsNumber).getId());

				}
			});
		}
	}

	private void addSendPeopleGoodsArchives(Integer sendPeopleId, Integer goodsArchivesId) {
		JfSendPeopleGoodsArchives jfSendPeopleGoodsArchives = new JfSendPeopleGoodsArchives();
		jfSendPeopleGoodsArchives.setGoodsArchivesId(goodsArchivesId);
		jfSendPeopleGoodsArchives.setSendPeopleId(sendPeopleId);
		jfSendPeopleGoodsArchivesMapper.insertSelective(jfSendPeopleGoodsArchives);
	}

	/**
	 * 
	 * <p>
	 * Title: sendGift
	 * </p>
	 * <p>
	 * 功能描述: 发送大礼包
	 * </p>
	 * 
	 * @param peopletels
	 * @param content
	 * @param goodsNumbers
	 * @param userId
	 */
	private void sendGift(Set<String> peopletels, String content, List<String> goodsNumbers, Long userId,
			Integer range) {
		JfGift jfGift = addGift();
		for (String goodsNumber : goodsNumbers) {
			addGiftGoods(jfGift.getId(), getGoodsArchives(goodsNumber).getId());
		}

		ExecutorService executor = Executors.newFixedThreadPool(10);
		for (String peopletel : peopletels) {
			executor.execute(new Runnable() {

				@Override
				public void run() {
					final JfSendPeople sendPeople = addSendPeople(peopletel, content, userId, range);
					addSendPeopleGift(sendPeople.getId(), jfGift.getId());
				}
			});
		}
		/*
		 * JfSendPeople sendPeople = null; for (String peopletel : peopletels) {
		 * sendPeople = addSendPeople(peopletel, content, userId);
		 * addSendPeopleGift(sendPeople.getId(), giftId); }
		 */
	}

	private JfSendPeople addSendPeople(String peopletel, String content, Long userId, Integer range) {
		if (range == null) {
			range = 1;
		}
		JfSendPeople jfSendPeople = new JfSendPeople();
		jfSendPeople.setAuthUserId(userId.intValue());
		jfSendPeople.setContent(content);
		jfSendPeople.setCreateTime(new Date());
		jfSendPeople.setPeopletel(peopletel);
		jfSendPeople.setSendRange((short) range.intValue());
		jfSendPeopleMapper.insertSelective(jfSendPeople);
		return jfSendPeople;
	}

	private void addSendPeopleGift(Integer sendPeopleId, Integer giftId) {
		JfSendPeopleGift jfSendPeopleGift = new JfSendPeopleGift();
		jfSendPeopleGift.setGiftId(giftId);
		jfSendPeopleGift.setSendPeopleId(sendPeopleId);
		jfSendPeopleGiftMapper.insertSelective(jfSendPeopleGift);
	}

	private void addGiftGoods(Integer giftId, Integer goodsArchivesId) {
		JfGiftGoodsArchives jfGiftGoodsArchives = new JfGiftGoodsArchives();
		jfGiftGoodsArchives.setCreateTime(new Date());
		jfGiftGoodsArchives.setGiftId(giftId);
		jfGiftGoodsArchives.setGoodsArchiveId(goodsArchivesId);
		jfGiftGoodsArchivesMapper.insertSelective(jfGiftGoodsArchives);
	}

	private JfGift addGift() {
		JfGift jfGift = new JfGift();
		jfGift.setCreateTime(new Date());
		jfGiftMapper.insertSelective(jfGift);
		return jfGift;
	}

	/**
	 * 
	 * <p>
	 * Title: getPeopletels
	 * </p>
	 * <p>
	 * 功能描述: 获取学校下用户手机号
	 * </p>
	 * 
	 * @param schoolNameSet
	 * @param range
	 *            范围 0所有 1教师 2家长
	 * @return
	 */
	@Override
	public Set<String> getPeopletels(Set<String> schoolNameSet, Integer range) {
		// 默认查询教师, 0所有 1教师 2家长
		if (range == null) {
			range = 1;
		}
		Set<String> peopletelSet = new HashSet<>();
		// 查询教师
		if (range == 1) {
			JxtResult result = jxtService.getTeachersBySchools(StringUtils.join(schoolNameSet, ","));
			if (result.getMsg() != null) {
				String[] peopletels = result.getMsg().split(",");
				for (String peopletel : peopletels) {
					peopletelSet.add(peopletel);
				}
			}
			// 查询家长
		} else if (range == 2) {
			List<String> peopletels = getGuardiansMobileBySchools(schoolNameSet);
			if (!CollectionUtils.isEmpty(peopletels)) {
				peopletelSet.addAll(peopletels);
			}
		}
		return peopletelSet;
	}

	/**
	 * <p>
	 * Title: getGuardiansMobileBySchools
	 * </p>
	 * <p>
	 * 功能描述: 获取学校下的所有家长手机号
	 * </p>
	 * 
	 * @param schoolNameSet
	 * @return
	 */
	private List<String> getGuardiansMobileBySchools(Set<String> schoolNameSet) {
		Map<String, Object> map = new HashMap<>();
		map.put("schoolNames", schoolNameSet);
		return sqlSessionTemplate.selectList("StdPeopleinfoExtendSql.getPeopletelsBySchoolNames", map);
	}

	/**
	 * <p>
	 * Title: getSchoolNameByRegions
	 * </p>
	 * <p>
	 * 功能描述: 获取地区下的所有学校名称
	 * </p>
	 * 
	 * @param regions
	 * @return
	 */
	private Set<String> getSchoolNameByRegions(List<RegionInfo> regions) {
		Set<String> schoolNameSet = new HashSet<>();
		Map<String, Object> map = null;
		List<Schoolinfo> schoolinfoList = null;
		for (RegionInfo region : regions) {
			map = new HashMap<>();
			map.put("province", region.getProvince());
			map.put("city", region.getCity());
			map.put("town", region.getTown());
			schoolinfoList = sqlSessionTemplate.selectList("RptMapRegionExtendSql.getSchoolsByRegion", map);
			if (!CollectionUtils.isEmpty(schoolinfoList)) {
				for (Schoolinfo schoolinfo : schoolinfoList) {
					schoolNameSet.add(schoolinfo.getSchoolname());
				}
			}
		}
		return schoolNameSet;
	}

	/**
	 * 
	 * <p>
	 * Title: getGoodsArchives
	 * </p>
	 * <p>
	 * 功能描述: 通过商品档案序列号获取商品档案
	 * </p>
	 * 
	 * @param goodsNumber
	 * @return
	 */
	private JfGoodsArchives getGoodsArchives(String goodsNumber) {
		JfGoodsArchivesExample jfGoodsArchivesExample = new JfGoodsArchivesExample();
		jfGoodsArchivesExample.createCriteria().andUuidEqualTo(goodsNumber);
		List<JfGoodsArchives> goodsArchivesList = jfGoodsArchivesMapper.selectByExample(jfGoodsArchivesExample);
		if (!CollectionUtils.isEmpty(goodsArchivesList)) {
			return goodsArchivesList.get(0);
		}
		return null;
	}

	/**
	 * <p>
	 * Title: sendGoodsByPeoples
	 * </p>
	 * <p>
	 * 功能描述: 指定用户发券
	 * </p>
	 * 
	 * @param sendGoodsForm
	 * @param userId
	 * @return
	 */
	@Override
	public String sendGoodsByPeoples(SendGoodsForm sendGoodsForm, Long userId) {
		if (sendGoodsForm == null) {
			return "参数为空";
		}
		if (StringUtils.isBlank(sendGoodsForm.getContent())) {
			return "发送内容为空";
		}
		if (CollectionUtils.isEmpty(sendGoodsForm.getGoodsNumber())) {
			return "商品编号为空";
		}
		if (CollectionUtils.isEmpty(sendGoodsForm.getPeopletels())) {
			return "发送对象为空";
		}
		if (sendGoodsForm.getGoodsNumber().size() > 5) {
			return "一次最多只能发放5个商品";
		}
		if (sendGoodsForm.getRange() != null && sendGoodsForm.getRange() != 1 && sendGoodsForm.getRange() != 2) {
			return "发放类型有误";
		}
		// 校验商品编号
		String result = checkGoods(sendGoodsForm.getGoodsNumber());
		if (result != null) {
			return result;
		}
		// 校验手机号
		for (String peopletel : sendGoodsForm.getPeopletels()) {
			if (getPeopleinfoByTel(peopletel) == null) {
				return String.format("手机号为%s的用户不存在", peopletel);
			}
		}

		Set<String> peopletelSet = new HashSet<>();
		peopletelSet.addAll(sendGoodsForm.getPeopletels());

		// 判断商品库存是否充足
		checkGoodsNumbers(sendGoodsForm.getGoodsNumber(), peopletelSet);
		// 判断输入的商品是否重复
		checkGoodsNumberRepeat(sendGoodsForm.getGoodsNumber());

		// 判断该用户是否存在相同的可领优惠券或已领取过相同的优惠券
		checkIeReceived(peopletelSet, sendGoodsForm.getGoodsNumber());

		if (sendGoodsForm.getGoodsNumber().size() == 1) {
			// 发送单个商品
			sendGoodsArchives(peopletelSet, sendGoodsForm.getContent(), sendGoodsForm.getGoodsNumber().get(0), userId,
					sendGoodsForm.getRange());
		} else {
			// 发送多个商品时，为大礼包
			sendGift(peopletelSet, sendGoodsForm.getContent(), sendGoodsForm.getGoodsNumber(), userId,
					sendGoodsForm.getRange());
		}
		// 保存发券记录
		addSendGoodsRecord(sendGoodsForm, userId);

		push(peopletelSet, sendGoodsForm.getContent(), sendGoodsForm.getGoodsNumber(), sendGoodsForm.getRange());
		return null;
	}

	private void checkGoodsNumberRepeat(List<String> goodsNumberList) {
		Set<String> set = new HashSet<>();
		set.addAll(goodsNumberList);
		if (set.size() < goodsNumberList.size()) {
			throw new OpServiceException("输入的商品编号重复");
		}
	}

	private String checkGoods(List<String> goodsNumberList) {
		JfGoodsArchives jfGodosArchives = null;
		for (String goodsNumber : goodsNumberList) {
			jfGodosArchives = getGoodsArchives(goodsNumber);
			if (jfGodosArchives == null) {
				return String.format("编号为%s的商品不存在或已禁用", goodsNumber);
			}
			if (jfGodosArchives.getConsumeCredit() != 0) {
				return String.format("编号为%s的商品不是免费商品", goodsNumber);
			}
			if (jfGodosArchives.getAvailableNum() == 0) {
				return String.format("编号为%s的商品库存不足", goodsNumber);
			}

			// 判断商品是否已通过审核
			if (!"2".equals(String.valueOf(jfGodosArchives.getAuditStatus()))) {
				return String.format("编号为%s的商品审核未通过", goodsNumber);
			}

			// 判断商品是否已过期
			if (jfGodosArchives.getEndTime() != null && jfGodosArchives.getEndTime().compareTo(new Date()) != 1) {
				return String.format("编号为%s的商品已过期", goodsNumber);
			}
		}
		return null;
	}

	private String checkGoods(List<String> goodsNumberList, Integer range, List<RegionInfo> regionList) {
		JfGoodsArchives jfGodosArchives = null;
		for (String goodsNumber : goodsNumberList) {
			jfGodosArchives = getGoodsArchives(goodsNumber);
			if (jfGodosArchives == null) {
				return String.format("编号为%s的商品不存在或已禁用", goodsNumber);
			}
			if (jfGodosArchives.getConsumeCredit() != 0) {
				return String.format("编号为%s的商品不是免费商品", goodsNumber);
			}
			if (jfGodosArchives.getAvailableNum() == 0) {
				return String.format("编号为%s的商品库存不足", goodsNumber);
			}

			// 判断商品是否已通过审核
			if (!"2".equals(String.valueOf(jfGodosArchives.getAuditStatus()))) {
				return String.format("编号为%s的商品审核未通过", goodsNumber);
			}

			// 判断商品是否已过期
			if (jfGodosArchives.getEndTime() != null && jfGodosArchives.getEndTime().compareTo(new Date()) != 1) {
				return String.format("编号为%s的商品已过期", goodsNumber);
			}
			// 校验商品投放地区和对象
			String result = checkRegionAndRange(jfGodosArchives.getId(), range, goodsNumber, regionList);
			if (result != null) {
				return result;
			}
		}
		return null;
	}

	private String checkRegionAndRange(Integer goodsArchiveId, Integer range, String goodsNumber,
			List<RegionInfo> regionList) {
		// 校验商品是否投放
		JfPutAreaExample jfPutAreaExample = new JfPutAreaExample();
		jfPutAreaExample.createCriteria().andGoodsArchivesIdEqualTo(goodsArchiveId);
		List<JfPutArea> areaList = jfPutAreaMapper.selectByExample(jfPutAreaExample);
		if (CollectionUtils.isEmpty(areaList)) {
			return String.format("编号为%s的商品还未投放地区", goodsNumber);
		} else {
			List<String> pUuidList = null;
			for (RegionInfo region : regionList) {
				// 获取发送地区的所有父级机构id
				pUuidList = regionService.getPUuidsByOrgName(region.getProvince(), region.getCity(), region.getTown());
				if (CollectionUtils.isEmpty(pUuidList)) {
					return String.format("%s%s%s不存在对应的机构id", region.getProvince(), region.getCity(), region.getTown())
							.replaceAll("null", "");
				} else {
					// 判断商品投放的机构id是否包含发送的地区，且该商品已经投放到对应的对象
					boolean flag = false;
					for (JfPutArea putArea : areaList) {
						if (pUuidList.contains(putArea.getAreaId())
								&& (String.valueOf(range).equals(String.valueOf(putArea.getPutRange()))
										|| "0".equals(String.valueOf(putArea.getPutRange())))) {
							flag = true;
						}
					}
					if (!flag) {
						return String.format("%s的商品不能投放到%s%s%s", goodsNumber, region.getProvince(), region.getCity(),
								region.getTown()).replaceAll("null", "");
					}
				}
			}
		}
		return null;
	}

	private StdPeopleinfo getPeopleinfoByTel(String peopletel) {
		StdPeopleinfoExample stdPeopleinfoExample = new StdPeopleinfoExample();
		stdPeopleinfoExample.createCriteria().andPeopletelEqualTo(peopletel).andIsdeletedEqualTo(false);
		List<StdPeopleinfo> stdPeopleinfoList = stdPeopleinfoMapper.selectByExample(stdPeopleinfoExample);
		if (CollectionUtils.isEmpty(stdPeopleinfoList)) {
			return null;
		} else {
			return stdPeopleinfoList.get(0);
		}
	}

	@Override
	public PageInfo<ExtSendGoodsRecord> pageSendGoodsRecord(int page, int size, String startTime, String endTime,
			String content) {
		PageHelper.startPage(page, size);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startTime", startTime);
		map.put("content", content);
		map.put("endTime", endTime);
		return new PageInfo<ExtSendGoodsRecord>(
				sqlSessionTemplate.selectList("SendGoodsExtendSql.listSendGoodsRecord", map));
	}

	@Override
	public ExtSendGoodsRecord getSendGoodsRecord(Integer recordId) {
		if (recordId == null) {
			return null;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("recordId", recordId);
		return sqlSessionTemplate.selectOne("SendGoodsExtendSql.listSendGoodsRecord", map);
	}

	@Override
	public List<ExtSendPeople> getCoupons(String peopletel) {
		if (StringUtils.isEmpty(peopletel)) {
			return null;
		}
		List<ExtSendPeople> list = new ArrayList<>();
		List<ExtSendPeople> giftList = sqlSessionTemplate.selectList("SendGoodsExtendSql.listSendPeopleGift",
				peopletel);
		if (!CollectionUtils.isEmpty(giftList)) {
			list.addAll(giftList);
		}
		List<ExtSendPeople> cardList = sqlSessionTemplate.selectList("SendGoodsExtendSql.listSendPeopleCard",
				peopletel);
		if (!CollectionUtils.isEmpty(cardList)) {
			list.addAll(cardList);
		}
		return list;
	}

}
